// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Element/CMASS4Parser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CMASS4);
/*
// file://./../../Doc/MarkDown/BulkData/CMASS4/CMASS4.md>
*/
using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
https://note.youdao.com/s/2rSeDOa5
*/

// 以下需要修改
enum CMASS4key : std::uint8_t
{
    CMASS4KEY_EID = 2,
    CMASS4KEY_M   = 3,  // Property identification number of a PROD entry. (Integer > 0; Default = EID)
    CMASS4KEY_S1  = 4,
    CMASS4KEY_S2  = 5,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseCMASS4 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建临时的CMASS4数据对象
    SG::DataStructure::FEM::CMASS4Data tempCMASS4;

    // TODO(bug 001) m_dominId的正确填写
    tempCMASS4.m_domainId = 1;  // 暂时设置m_domainId为1，待修复

    int curIDKey = 1;           // 当前字符串是第几项

    // 遍历所有分割后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 判断当前词条是否为空白
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 如果为空白，跳过当前词条
            continue;
        }

        // 将当前字符串转换为科学计数法的浮点数

        // 根据当前字段的索引，处理不同的CMASS4属性
        switch (curIDKey)
        {
        case CMASS4KEY_EID:
            // 解析并存储元素识别号EID
            tempCMASS4.m_id = convertToI64 (curStrKey);
            break;
        case CMASS4KEY_M:
            // 解析并存储属性识别号PID
            tempCMASS4.m_M = convertToReal (curStrKey);
            break;
        case CMASS4KEY_S1:
            // 解析并存储网格点G1
            tempCMASS4.m_g[0] = convertToI64 (curStrKey);
            break;
        case CMASS4KEY_S2:
            // 解析并存储网格点G2
            tempCMASS4.m_g[1] = convertToI64 (curStrKey);
            break;
        default:
            break;
        }

        // 处理完当前词条后，索引加1
        curIDKey++;
    }

    // 将解析后的CMASS4数据添加到数据库中
    return dbServiceFactory->get<IElementService> ()->Add (tempCMASS4);
}